/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.linkwithweb.products.swiftui.context.SystemContext;

/**
 * @author ashwin
 * 
 */
public class DateUtils {

	private static DateUtils dateUtils = new DateUtils();

	private String defaultDateFormat = "dd-MMM-yyyy hh:mm:ss";

	private DateUtils() {

	}

	/**
	 * @return
	 */
	public static DateUtils getInstance() {
		return dateUtils;
	}

	/**
	 * @param args
	 */
	public static void main(String args[]) {
		getInstance().test();
	}

	public void test() {
		System.out.println(testDateFormat("dd MMMMM yyyy"));
		System.out.println(testDateFormat("yyyyMMdd"));
		System.out.println(testDateFormat("dd.MM.yy"));
		System.out.println(testDateFormat("MM/dd/yy"));
		System.out.println(testDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
		System.out.println(testDateFormat("yyyy-MM-dd hh:mm:ss"));
		System.out.println(testDateFormat("dd-MMM-yyyy hh:mm:ss"));
		System.out.println(testDateFormat("EEE, MMM d, ''yy"));
		System.out.println(testDateFormat("h:mm a"));
		System.out.println(testDateFormat("H:mm:ss:SSS"));
		System.out.println(testDateFormat("K:mm a,z"));
		System.out.println(testDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
	}

	/**
	 * @param format
	 * @return
	 */
	public String testDateFormat(String format) {
		Date today = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String datenewformat = formatter.format(today);
		return datenewformat;
	}

	/**
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public Date getDate(String dateString) throws ParseException {
		return getDate(dateString, defaultDateFormat);
	}

	/**
	 * @param dateString
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public Date getDate(String dateString, String format) throws ParseException {
		try {
			if (format == null)
				format = SystemContext.getInstance().getUserContext().getFrameworkUser().getDefaultDateFormat();
			if (format == null)
				format = defaultDateFormat;
		} catch (Exception e) {
			// TODO: handle exception
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.parse(dateString);
	}

	/**
	 * @param dateValue
	 * @param format
	 * @return
	 */
	public String getDateString(Date dateValue, String format) {
		try {
			if (format == null)
				format = SystemContext.getInstance().getUserContext().getFrameworkUser().getDefaultDateFormat();
			if (format == null)
				format = defaultDateFormat;
		} catch (Exception e) {
			// TODO: handle exception
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(dateValue);
	}

}
